<template>
	<div>
		<div class="box">
			<uni-forms
				ref="form"
				:model="currentInfoData"
				validate-trigger="bind"
				label-width="0">
				<uni-table border stripe emptyText="暂无更多数据" class="table">
					<uni-tr>
						<uni-th align="center" width="70px">操作</uni-th>
						<uni-th align="center">费用项</uni-th>
						<uni-th align="center" width="100px">次数</uni-th>
						<uni-th align="center" width="100px">费用金额</uni-th>
						<uni-th align="center" width="100px">备注</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in currentInfoData.feeList">
						<uni-td align="center" class="td">
							<div>
								<div @click="remove_feeList(item, index)">
									删除
								</div>
							</div>
						</uni-td>
						<uni-td align="center" class="td">
							<FromItem
								:showRequiredIcon="false"
								:required="true"
								:state="state"
								errorLabel="其他费用项"
								:name="['feeList', index, 'feeKey']">
								<ASelectUni
									:dict="{
										list: dict.type.quota_fee,
									}"
									:disabled="state == 'disabled'"
									v-model="item.feeKey"
									:state="state"
									@change="
										change_feeKey(item, arguments)
									"></ASelectUni>
							</FromItem>
						</uni-td>
						<uni-td align="center" class="td">
							<FromItem
								:showRequiredIcon="false"
								:required="true"
								:state="state"
								errorLabel="其他费用项数量"
								:name="['feeList', index, 'feeCount']">
								<AInput
									:state="state"
									v-model="item.feeCount"
									type="number"></AInput>
							</FromItem>
						</uni-td>
						<uni-td align="center" class="td">
							<FromItem
								:showRequiredIcon="false"
								:required="true"
								:state="state"
								errorLabel="其他费用项单价"
								:name="['feeList', index, 'feeAmount']">
								<AInput
									:state="state"
									v-model="item.feeAmount"
									type="number"
									placeholder="请输入金额"></AInput>
							</FromItem>
						</uni-td>
						<uni-td align="center" class="td">
							<AInput
								v-model="item.remark"
								:state="state"></AInput>
						</uni-td>
					</uni-tr>
				</uni-table>
			</uni-forms>
			<button
				type="primary"
				v-if="state === 'write'"
				@click="push_feeList">
				增加
			</button>
		</div>
	</div>
</template>

<script>
	import * as app from '@/api/index.js';
	import * as apis from '@/api/quota/apply/api.js';
	import * as utils from '@/pages/quota/libs/utils.js';
	import FromItem from '../formItem/index.vue';
	export default {
		name: '',
		components: { FromItem },
		dicts: ['quota_fee'],
		props: {},
		inject: [
			'details_provide',
			'config_provide',
			'deptList_provide',
			'state_provide',
			'index_provide',
			'planDicts_provide',
			'fromtype_provide',
		],
		data() {
			return {
				dictList: {
					refittedList: [{ label: 1, value: 1, price: 1 }],
				},
			};
		},
		computed: {
			details() {
				return this.details_provide();
			},
			config() {
				return this.config_provide();
			},
			deptList() {
				return this.deptList_provide();
			},
			planDicts() {
				return this.planDicts_provide();
			},
			state() {
				return this.state_provide();
			},
			fromtype() {
				return this.fromtype_provide();
			},

			index() {
				return this.index_provide();
			},
			currentInfoData() {
				return this.details.productList?.[this.index] ?? {};
			},
			firstInfoData() {
				return this.details.productList?.[0] ?? {};
			},
		},
		watch: {},
		filters: {},
		created() {},
		mounted() {},
		methods: {
			push_feeList() {
				if (Array.isArray(this?.currentInfoData?.feeList) == false) {
					this.$set(this.currentInfoData, 'feeList', []);
				}
				this.currentInfoData.feeList.push({
					feeKey: undefined,
					feeVal: undefined,
					feeCount: undefined,
					feeAmount: undefined,
					remark: undefined,
				});
			},
			change_feeKey(row, [{ val, item }]) {
				row.feeVal = item.label;
				this.setDisabled();
			},

			remove_feeList(item, index) {
				uni.showModal({
					content: `是否删除${
						item.feeVal ? item.feeVal : '该'
					}加装项？`,
					success: res => {
						if (res.confirm) {
							this.currentInfoData.feeList.splice(index, 1);
						}
					},
				});
			},
			setDisabled(row) {
				this.quotaFeeList?.map((item, i) => {
					this.$set(item, 'disabled', false);
					let child = this.currentInfoData.feeList.find(e => {
						return e.feeKey == item.value;
					});
					if (child && child.feeVal !== '其他') {
						this.$set(item, 'disabled', true);
					}
				});
			},
		},
		beforeDestroy() {
			if (this.notEmpty(this.details?.productList?.[this.index])) {
				this.currentInfoData.feeList = [];
			}
		},
	};
</script>

<style lang="scss" scoped>
	.box {
		// margin: 20rpx;
		// padding: 20rpx;
		padding-top: 20rpx;
		background-color: #fff;
		border-radius: 8rpx;
		.title {
			border-bottom: 1px solid #e9e9e9;
			font-weight: 500;
			padding-bottom: 10rpx;
			margin-bottom: 10rpx;
		}
	}
</style>
